<?php
// +----------------------------------------------------------------------
// | 日志分类管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: CategoryAction.class.php 110 2011-05-06 10:22:31Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class CategoryAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	//添加分类页面
	public function addCat() {
		$cat = D('Category');
		$cat_option = $cat->getCatList(0,0,0,true);
		$this->assign('action_link', array('href'=>__URL__.'/listCat/', 'text' => L('listCat')));
		$this->assign('ur_here',L('addCat'));
		$this->assign('default_order', 50);
		$this->assign('cat_option', $cat_option);
		$this->display();
	}
	
	//添加分类处理
	public function insertCat() {		
		if ($_REQUEST['act']) {	
			$cat = D('Category');
			if (false === $cat->create()) {
				$this->error($cat->getError());
			} else {
				if ($result = $cat->add()) {
					clear_cache_files();    // 清除缓存
					set_log('add(Category, Id: '.$result.')');
					$this->assign('jumpUrl',__URL__.'/listCat/');
					$this->success(L('success'));
				} else {
					$this->error(L('error'));
				}
			}
		} else {
			$this->error(L('illegal'));
		}	
	}
	
	//分类列表
	public function listCat() {
		$cat = D('Category');
		$list = $cat->getCatList(0,0,0,false);
		$this->assign('action_link', array('href'=>__URL__.'/addCat/', 'text' => L('addCat')));
		$this->assign('ur_here',L('listCat'));
		$this->assign('list', $list);
		$this->display();
	}
	
	//编辑分类页面
	public function editCat() {
		$cat = D('Category');
		$id = $_REQUEST['id'];
		$vo	= $cat->getByCategoryId($id);
		$cat_option = $cat->getCatList(0,$vo['parent_id'] ,0,true);	
		$this->assign('action_link', array('href'=>__URL__.'/listCat/', 'text' => L('listCat')));
		$this->assign('ur_here',L('editCat'));
		$this->assign('vo',$vo);
		$this->assign('cat_option', $cat_option);
		$this->display();
	}
	
	//编辑分类信息
	public function updateCat() {
		$cat = D ('Category');
		$list = $cat->create();	
		if (false === $list) {
			$this->error($cat->getError());
		} else {
			$list['id'] = $_POST['category_id'];
			$list['parent_id'] = $_POST['parent_id'];
			$path = ROOT_PATH.STATIC_CACHES_DIR;
			if (file_exists($path.'category_static.php')) {
				$data = f_static_cache('category_static','',$path);
			} else {
				$data = $cat->getCatArray();
			}	
			$arr = $cat->getCatOptions($data,$list['id']);
			
			$children = array_keys($arr);     // 获得当前分类的所有下级
			
			if (in_array($list['parent_id'], $children)) {
				// 选定的父类是当前分类或当前分类的下级分类
			   $this->error(L('is_leaf_error'));
			}
			
			$result = $cat->save($list);
			if (false !== $result) {
				clear_cache_files(); // 清除缓存
				set_log('edit(Category, Id: '.$cat->category_id.')');
				$this->assign('jumpUrl',__URL__.'/listCat/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		}
	}
	
	//批量转移日志分类页面
	public function movePosts() {
		$cat = D('Category');
		$id = !empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		$cat_option = $cat->getCatList(0,$id,0,true);
		$this->assign('action_link', array('href'=>__URL__.'/listCat/', 'text' => L('listCat')));
		$this->assign('ur_here', L('movePosts'));
		$this->assign('cat_option', $cat_option);
		$this->display();		
	}
	
	//处理批量转移日志分类的处理程序
	public function actionMove() {
		if ($_REQUEST['act']) {	
			$p = M('Posts');
			$cat_id        = !empty($_POST['cat_id'])        ? intval($_POST['cat_id'])        : 0;
			$target_cat_id = !empty($_POST['target_cat_id']) ? intval($_POST['target_cat_id']) : 0;
			/* 日志分类不允许为空 */
			if ($cat_id == 0 || $target_cat_id == 0) {
				$this->error(L('cat_move_empty'));
			}
			
			if ($list = $p->where("category_id=$cat_id")->setField('category_id', $target_cat_id)) {
				clear_cache_files(); // 清除缓存
				set_log('move(Posts, From CatId: '.$cat_id.' To CatId '.$target_cat_id.')');
				$this->assign('jumpUrl',__URL__.'/listCat/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		} else {
			$this->error(L('illegal'));
		}	
	}
	
	//删除日志分类
	public function dropCat() {
		$cat = M('Category');
		$p = M('Posts');
		$cat_id  = !empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
		 /* 当前分类下是否有子分类 */
		$cat_count = $cat->where("parent_id=$cat_id")->count();

		/* 当前分类下是否存在日志 */
		$posts_count = $p->where("category_id=$cat_id")->count();  

		/* 如果不存在下级子分类和商品，则删除之 */
		if ($cat_count == 0 && $posts_count == 0) {
			/* 删除分类 */		
			if ($cat->where("category_id=$cat_id")->delete()) {
				clear_cache_files(); // 清除缓存
				set_log('drop(Category, Id: '.$cat_id.')');
				$this->assign('jumpUrl',__URL__.'/listCat/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		} else {
			$this->error (L('cat_isleaf'));
		}	
	}
	
	//固定链接处理
	public function ajaxCat() {
		header('Content-type:text/html;charset=utf-8');
		$cat = D ('Category');
		$t = $_GET['title'];
		$c = $_GET['cat'];
		echo urlencode(strtolower($cat->getCatUrl($c).$t));
	}

	//ajax快速切换分类显示状态,用户感极强
	public function visibleCat() {
		if ($_REQUEST['act']) {	
			$map	=	array();
			$map['category_id']	 =	$_REQUEST['id'];
			$val = ($_REQUEST['val'] == 0) ? 0 : 1;
			$cat = M('Category');
			if ($list = $cat->where($map)->setField('category_isshow', $val)) {
				clear_cache_files(); // 清除缓存
				set_log('edit(Category, Toggle, Id: '.$_REQUEST['id'].')');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		} else {
			$this->error(L('illegal'));
		}		
	}
	
}

?>